home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 05 / 6 / DISK0564.ZIP / SOURCE.ARC / DETAB.C < prev    next >
C/C++ Source or Header  |  1988-07-10  |  2KB  |  73 lines

  1. /* this program replaces tabs in a file with the equivalent number of
  2.    spaces (tabs are assumed to be set every 8 columns). */ 
  3.    
  4. /* It reads from a file if a file name is specified, or from the
  5.    standard input if no file name is given.  It writes to the standard
  6.    output */
  7.  
  8. /* by Jon Dart, 3012 Hawthorn St., San Diego, CA 92104 */
  9. /* version 1.0, 16-Nov-86 */
  10.  
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13.  
  14. #define PATHSIZE     65    /* max size of DOS pathname + 1 */
  15. #define BUFSIZE        16384   /* input buffer size */
  16. #define EOF        -1
  17. #define TAB             (unsigned char) '\011'
  18. #define BKS        (unsigned char) '\010'
  19. #define CTRLZ           (unsigned char) '\032'
  20.  
  21. extern char *malloc();
  22.  
  23. main(argc,argv)
  24. int argc; char **argv;
  25. {
  26.     FILE *infd;    
  27.     unsigned int c2;
  28.     register int c,charpos,nexttab;
  29.     unsigned char *buffer;
  30.  
  31.     buffer = (unsigned char *)  malloc(BUFSIZE);
  32.  
  33.     if (argc>=2) {
  34.         infd = fopen(argv[1],"r");
  35.         if (infd == NULL) {
  36.             fprintf(stderr,"\nCan't open: %s\n",argv[1]);
  37.             exit(1);
  38.         }
  39.     }
  40.     else /* read from std input */
  41.         infd = stdin;
  42.     setbuf(infd,buffer);
  43.     
  44.     charpos = 1;
  45.     while ((c=getc(infd)) != EOF) {
  46.         c2 = c & 0177;
  47.     if (c2==CTRLZ) 
  48.         break;
  49.         else if (c2==TAB)  {
  50.            if (charpos % 8 == 0) 
  51.                nexttab = charpos + 1;
  52.            else
  53.                nexttab = ((charpos / 8) + 1)*8 + 1;
  54.            while (charpos < nexttab) {
  55.                ++charpos;
  56.                putc(' ',stdout);
  57.            }
  58.     }
  59.         else {
  60.             if ((c2==BKS) && (charpos>1)) 
  61.                 --charpos;
  62.             else if (c2=='\n') 
  63.                 charpos = 1;
  64.             else if (c2>=32) 
  65.                 ++charpos;
  66.             putc(c2,stdout);
  67.         }
  68.     }
  69.     if (ferror(infd)) fprintf(stderr,"detab: error in reading file\n");
  70.     if (ferror(stdout)) fprintf(stderr,"detab: error in writing file\n");
  71.     fclose(infd);
  72. }
  73.